#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>


int setVal(char *word);
int getVal(char *word);
void load(char *filename);
FILE * serializeFile;
fpos_t* place[4000000];

int _indeks_put(int w, int a, int p);

void _indeks_deserialize(char *);
int _start_structure();
void _fully_indeks_serialize(char *);

void create(const char *filename) {
   FILE *f = fopen(filename, "r");
   char c;
   char dummy[1000];
   int val = 0;
   int position = 0, article = 0, len = 0, isTitle = 0;
   while(( c = getc(f)) != EOF) {
      if(c == '?' && len > 0) {
         dummy[len] = 0;
         if(strcmp(dummy, "#title") == 0) {
            c = ' ';
         }       
      }
      if(c == ' ' || c == '\n') {
         if ( c == '\n' ){
            isTitle = 0;
         }
         if(len == 0)
            continue;
         dummy[len] = 0;
         len = 0;
         if(strcmp(dummy, "#title#") == 0) {
            isTitle = 1;         
            article++;
            position = 0;
            if( article % 10000 == 0 )
                printf("%d\n",article);
            continue;
         }
         
         position++;
         val = getVal(dummy);
         if(val == -1) {
            printf("to nie powinno sie zdarzyc! (nie ma w slowniku %s)", dummy);
            exit(1);
         }
         
         if( isTitle ){
           _indeks_put(val, article, 1);
         }
         else{
           _indeks_put(val, article, 2);
         }         
         
         continue;
      }
      if(c == '|') {
         dummy[len] = 0;
         len = 0;
         val = getVal(dummy);
         if(val == -1) {
            printf("to nie powinno sie zdarzyc! (nie ma w slowniku %s)", dummy);
            exit(1);
         }
         _indeks_put(val, article, position);
         continue;
      }
      dummy[len] = c;
      len++;
   }
   fclose(f);
}


#include "indeks.c"
#include "staticNode.c"
#include "indeksSerialize.c"

int serialize_function(char * fromWiki, char* fromIndex, char* fromNode, char* toIndex) {

   char *wiki = fromWiki;
   
   _indeks_deserialize(fromIndex);
   _start_structure();
   load(fromNode);
   
   create(wiki);
   printf("indeks created!\n");
   
   //page_fullSerialize();
   _fully_indeks_serialize(toIndex);
   
   return 0;
}

int main(int argn, char** argv) {
   if( argn == 5 )
   {
        serialize_function(argv[1], argv[2], argv[3],argv[4]);
        return 0;
   }
   else if( argn == 3 )
   {
       serializeFile = fopen(argv[2],"wb");
       _fully_indeks_deserialize(argv[1]);
       printf("deserialize ok\n");
       page_fullSerialize();
      fclose(serializeFile);
      return 0;
   }
   else
   {
        printf("Use createIndex f1 f2 f3 f4 to create index using wiki file stored in f1, mock index stored in f2 and prefix tree (string -> int mapping) stored in f3. The result will be written to f4 \nUse createIndex f1 f2 to convert static index f1 to one that allows paging. The result will be written to f2\n");
        return 0;
   }
}

